package com.action;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.utils.ChgEcod;
import com.utils.RenameImg;
import com.utils.CutImage;
import com.utils.HibCommons;
import com.bean.User;

public class AdminUserAction extends ActionSupport {
	final static String PATH = ServletActionContext.getServletContext().getRealPath("/");

	private User user = new User();
	private List<User> admin_user;
	private File upload;
	private String uploadFileName;

	public String admin_user() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		username = new String(username.getBytes("ISO8859-1"), "UTF-8");

		HibCommons.OpenQuery();
		String hql = "FROM User where username = :username";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("username", username);
		admin_user = query.list();
		HibCommons.CloseQuery();
		Map map = ActionContext.getContext().getSession();
		setUser(admin_user.get(0));
		map.put("username", getUser());
		return SUCCESS;
	}

	public String admin_user_getValue() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		User getValue = new User();
		getValue.setId(request.getParameter("id"));
		getValue.setUsername(new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8"));
		getValue.setInfo(ChgEcod.change(request.getParameter("info")));
		getValue.setEmail(request.getParameter("email"));
		getValue.setPhone(request.getParameter("phone"));
		getValue.setImage(request.getParameter("image"));

		Map map = ActionContext.getContext().getSession();
		map.put("getValue", getValue);
		return SUCCESS;
	}

	public String u_alert() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		User u_alert = new User();
		u_alert.setUsername(request.getParameter("username"));
		u_alert.setInfo(user.getInfo());
		u_alert.setPhone(user.getPhone());
		u_alert.setEmail(user.getEmail());

		String hql = "UPDATE User u set " +
					 "u.info = :info ," +
					 "u.email = :email ," + 
					 "u.phone = :phone WHERE u.id = :id";
		HibCommons.OpenQuery();
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("info", u_alert.getInfo());
		query.setParameter("email", u_alert.getEmail());
		query.setParameter("phone", u_alert.getPhone());
		query.setParameter("id", user.getId());
		query.executeUpdate();
		HibCommons.CloseQuery();


		Map map = ActionContext.getContext().getSession();
		map.put("username", u_alert);
		return SUCCESS;
	}

	public String u_img_get() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		User img_get = new User();
		img_get.setId(request.getParameter("id"));
		img_get.setUsername(new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8"));
		img_get.setImage(request.getParameter("image"));

		Map map = ActionContext.getContext().getSession();
		map.put("img_getValue", img_get);
		return SUCCESS;
	}

	public String img_alert() throws Exception {
		InputStream is = new FileInputStream(upload);
		String name = RenameImg.getNewName(uploadFileName);
		if(!RenameImg.getExt(uploadFileName).equals(".jpg")){
			this.addFieldError("upload", "仅支持jpg格式图片！");
			return INPUT;
		}
		OutputStream os = new FileOutputStream(PATH + "img/" + name);
		byte buffer[] = new byte[1024];
		int count = 0;
		while ((count = is.read(buffer)) > 0) {
			os.write(buffer, 0, count);
		}
		os.close();
		is.close();

		Map map = ActionContext.getContext().getSession();
		user.setImage(name);
		map.put("upload_ok", user);
		return SUCCESS;
	}

	public String update_logo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8");

		int x = Double.valueOf(user.getX()).intValue();
		int y = Double.valueOf(user.getY()).intValue();
		int width = Double.valueOf(user.getWidth()).intValue();
		int height = Double.valueOf(user.getHeight()).intValue();

		String image_name = CutImage.cut(request.getParameter("name_img"), x, y, width, height);

		HibCommons.OpenQuery();
		String hql = "UPDATE User u set u.image = :image WHERE u.username = :username";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("image", image_name);
		query.setParameter("username", username);
		query.executeUpdate();
		HibCommons.CloseQuery();
		
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		map.put("username", user);
		return SUCCESS;

	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getAdmin_user() {
		return admin_user;
	}

	public void setAdmin_user(List<User> admin_user) {
		this.admin_user = admin_user;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}
